package com.ht.demo.service.manager.monitor.impl;

import com.ht.demo.common.appexception.AppErrorCodeEnum;
import com.ht.demo.common.appexception.AppException;
import com.ht.demo.common.page.PageBO;
import com.ht.demo.common.pojo.bo.monitor.LoginInfoBO;
import com.ht.demo.common.pojo.qto.monitor.LoginInfoQTO;
import com.ht.demo.dao.dao.monitor.LoginInfoDao;
import com.ht.demo.dao.domain.monitor.LoginInfoDO;
import com.ht.demo.service.convert.monitor.LoginInfoBeanUtil;
import com.ht.demo.service.manager.monitor.LoginInfoManager;
import com.ht.demo.service.system.UserTokenManager;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 系统登录日志，登录记录Service
 * 业务层处理
 * @author hdengxian
 * @date 2023-07-31
 */
@Component
public class LoginInfoManagerImpl implements LoginInfoManager {
    @Resource
    private LoginInfoDao loginInfoDao;
    @Resource
    private UserTokenManager userTokenManager;
    /**
     * 新增
     * 系统登录日志，登录记录
     * @param loginInfoBO 系统登录日志，登录记录
     * @return 结果
     */
    @Override
    public Integer insertData(LoginInfoBO loginInfoBO){
        if (loginInfoBO ==null) {
            throw new AppException("参数为空");
        }
        loginInfoBO.setCreateTime(new Date());
        LoginInfoDO saveData = LoginInfoBeanUtil.convertToDO(loginInfoBO);
        int count = loginInfoDao.insert(saveData);

        return count;
    }

    /**
     * 修改
     * 系统登录日志，登录记录
     * @param loginInfoBO 系统登录日志，登录记录
     * @return 结果
     */
    @Override
    public Boolean updateDataById(LoginInfoBO loginInfoBO) {
        if (loginInfoBO == null || loginInfoBO.getLoginInfoId() == null){
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        LoginInfoDO updateData = LoginInfoBeanUtil.convertToDO(loginInfoBO);
        Integer count = loginInfoDao.updateById(updateData);
        return Boolean.TRUE;
    }

    /**
     * 批量删除
     * 系统登录日志，登录记录
     * @param id 需要删除的系统登录日志，登录记录主键
     * @return 结果
     */
    @Override
    public Boolean deleteById(Long id) {
        if (id == null){
            return Boolean.FALSE;
        }
        loginInfoDao.deleteById(id);
        return Boolean.TRUE;
    }

    @Override
    public Boolean cleanAll() {
        loginInfoDao.deleteAll();
        return null;
    }

    @Override
    public Boolean forceQuit(String token) {
        userTokenManager.userExitLogin(token);
        return Boolean.TRUE;
    }

    public Boolean deleteByIds(Long[] ids){

        Integer count = loginInfoDao.deleteByIds(ids);
        return Boolean.TRUE;
    }

    /**
     * 查询
     * 系统登录日志，登录记录
     * @param id 系统登录日志，登录记录主键
     * @return 系统登录日志，登录记录
     */
    @Override
    public LoginInfoBO findById(Long id) {
        LoginInfoDO data = loginInfoDao.findById(id);
        return LoginInfoBeanUtil.convertToBO(data);
    }

    /**
     * 分页查询列表
     * 系统登录日志，登录记录
     * @param qto 系统登录日志，登录记录
     * @return 系统登录日志，登录记录
     */
    @Override
    public PageBO<LoginInfoBO> findDataForPage(LoginInfoQTO qto) {
        PageBO<LoginInfoBO> pageBO = new PageBO();
        if (qto == null) {
            return pageBO;
        }
        Integer count = loginInfoDao.findForPageCount(qto);
        if (count == null || count <= 0) {
            return pageBO;
        }
        List<LoginInfoDO> doList = loginInfoDao.findForPage(qto);
        pageBO.setData(LoginInfoBeanUtil.convertToBOList(doList), count);
        return pageBO;
    }


}
